package com.abc.recycling.service.impl;

import com.abc.recycling.domain.Message;
import com.abc.recycling.domain.MessageTemplate;
import com.abc.recycling.mapper.MessageTemplateMapper;
import com.abc.recycling.service.MessageService;
import com.abc.recycling.service.MessageTemplateService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@RequiredArgsConstructor
public class MessageTemplateServiceImpl implements MessageTemplateService {

    private final MessageTemplateMapper templateMapper;
    private final ObjectMapper objectMapper;
    
    private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{\\{(.*?)\\}\\}");

    @Override
    @Transactional
    public MessageTemplate createTemplate(MessageTemplate template) {
        template.setStatus(1);  // 默认启用
        template.setCreateTime(LocalDateTime.now());
        templateMapper.insert(template);
        return template;
    }

    @Override
    @Transactional
    public void updateTemplate(MessageTemplate template) {
        template.setUpdateTime(LocalDateTime.now());
        templateMapper.update(template);
    }

    @Override
    @Transactional
    public void deleteTemplate(Long id) {
        templateMapper.deleteById(id);
    }

    @Override
    public MessageTemplate getTemplate(Long id) {
        return templateMapper.findById(id);
    }

    @Override
    public List<MessageTemplate> searchTemplates(String type, Integer status, String keyword) {
        return templateMapper.search(type, status, keyword);
    }

    @Override
    @Transactional
    public void updateTemplateStatus(Long id, Integer status) {
        templateMapper.updateStatus(id, status);
    }

    @Override
    @Transactional
    public void sendMessageWithTemplate(String templateCode, Long userId, Map<String, Object> variables) {
        throw new UnsupportedOperationException("此方法已移至 MessageService");
    }
    
    /**
     * 替换模板中的变量
     */
    private String replaceVariables(String template, Map<String, Object> variables) {
        if (template == null || variables == null) {
            return template;
        }
        
        StringBuffer result = new StringBuffer();
        Matcher matcher = VARIABLE_PATTERN.matcher(template);
        
        while (matcher.find()) {
            String variable = matcher.group(1);
            Object value = variables.get(variable);
            matcher.appendReplacement(result, value != null ? value.toString() : "");
        }
        matcher.appendTail(result);
        
        return result.toString();
    }
    
    /**
     * 验证变量格式是否正确
     */
    private void validateVariables(String variables) {
        try {
            if (variables != null && !variables.isEmpty()) {
                objectMapper.readValue(variables, Map.class);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException("变量格式不正确");
        }
    }
} 